#include "motor.h"
#include "config.h"



//电机预设目标位置
u8 pos_target=POS_NOSET;

//设置电机目标位置
void setMotor(u8 POS){
    pos_target=POS;
}


//控制电机正转
void forward(void){
    //01
    pwm_SetChannel(s2,32000);       //先置1
    pwm_SetChannel(s1,MOTOR_SPEED);   
}

//控制电机反转
void backward(void){
	#ifdef SINGLE_DIRECTION
		forward();
	#else
		//10
		pwm_SetChannel(s1,32000);		//先置1
		pwm_SetChannel(s2,MOTOR_SPEED);
	#endif
}

//电机停止
void stop(void){
    //11
    pwm_SetChannel(s1,32000);
	pwm_SetChannel(s2,32000);
}


//电机控制，在主循环调用，使电机向预设位置移动
void motor_loop(void){   
	switch(pos_target){
        case POS_NOSET:
        stop();
        break;

        case POS_FORWARD_LIMIT:
		if(pos1_limit()==RESET)	//已到达位置
		{
			stop();
			pos_target=POS_NOSET;
		}
		else
        	forward();
        break;

        case POS_BACKWARD_LIMIT:
		if(pos2_limit()==RESET)	//已到达位置
		{
			stop();
			pos_target=POS_NOSET;
		}
		else
        	backward();
        break;
    }
}





